/**
 * Panel permettant la modification du dictionnaire personnel
 * 
 * @author Marie-Hélène Goddet, Damien Finck, Cédric Schaller
 * 
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import fastwriting.Config;
import fastwriting.Dico;

@SuppressWarnings("serial")
public class JDico extends JPanel {
	
	private ActionListener actionListener;
	
	protected Component horizontalStrut;
	protected JList jlst_list;protected JScrollPane jsp_list;
	protected JPanel jp_droite;
		protected JPanel jp_dico_perso;
			protected JPanel jp_ajout;
				protected JTextField jtf_ajout;
				protected JButton jb_ajout;
			protected JPanel jp_action;
				protected JButton jb_modifier;
				protected JButton jb_supprimer;
				protected JButton jb_select;
		protected JPanel jp_dico_general;
			protected JLabel jl_dico_general;
			protected JComboBox jcb_dico_general;
	
	protected DefaultListModel lm_dico;
	
	Dico dico;
	
	/**
	 * Constructeur
	 * @param dico le dictionnaire 
	 */
	public JDico(Dico dico) {
		this.dico=dico;
		this.lm_dico = new DefaultListModel();
		read_dico();
		
		String[] langues = {"Français"};
		
		/** Evénements **/
		ActionListener action = new Event_JPDico(this);
		/** Création des éléments     **/
		this.horizontalStrut = Box.createHorizontalStrut(40);
		this.jlst_list = new JList(this.lm_dico);this.jsp_list = new JScrollPane(this.jlst_list);
		this.jp_droite = new JPanel(new BorderLayout());
			this.jp_dico_perso = new JPanel(new BorderLayout());
				this.jp_ajout = new JPanel();
					this.jtf_ajout = new JTextField(20);
					this.jb_ajout = new JButton("Ajouter");
				this.jp_action = new JPanel();
					this.jb_modifier = new JButton("Modifier");
					this.jb_supprimer = new JButton("Supprimer");
					this.jb_select = new JButton("Selectionner");
		this.jp_dico_general = new JPanel();
			this.jl_dico_general = new JLabel("Langue :");
			this.jcb_dico_general = new JComboBox(langues);
		/** Paramétrage des éléments  **/
		Font f = new Font("Monospaced",0,12);
		this.jlst_list.setFont(f);
		//this.jlst_list.setVisibleRowCount(7);
		this.setLayout(new BorderLayout());
		this.jsp_list.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		this.jsp_list.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		this.jlst_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.jp_dico_general.setBorder(BorderFactory.createTitledBorder("Dictionnaire général"));
		this.jp_dico_perso.setBorder(BorderFactory.createTitledBorder("Dictionnaire personnalisé"));
		/** Action sur les éléments   **/
		this.jb_ajout.addActionListener(action);
		this.jb_modifier.addActionListener(action);
		this.jb_supprimer.addActionListener(action);
		this.jb_select.addActionListener(action);
		/** Montage des éléments      **/
		this.add(this.horizontalStrut,BorderLayout.WEST);
		this.add(this.jsp_list,BorderLayout.CENTER);
		this.add(this.jp_droite,BorderLayout.EAST);
			this.jp_droite.add(this.jp_dico_perso,BorderLayout.NORTH);
				this.jp_dico_perso.add(this.jp_ajout,BorderLayout.NORTH);
					this.jp_ajout.add(this.jtf_ajout);
					this.jp_ajout.add(this.jb_ajout);
				this.jp_dico_perso.add(this.jp_action,BorderLayout.SOUTH);
					this.jp_action.add(this.jb_modifier);
					this.jp_action.add(this.jb_supprimer);
					this.jp_action.add(this.jb_select);
			this.jp_droite.add(Box.createVerticalStrut(40),BorderLayout.CENTER);
			this.jp_droite.add(this.jp_dico_general,BorderLayout.SOUTH);
				this.jp_dico_general.add(this.jl_dico_general);
				this.jp_dico_general.add(this.jcb_dico_general);
	}
	public void addActionListener(ActionListener actionListener) {
		this.actionListener = actionListener;
	}
	public ActionListener getActionListener() {
		return actionListener;
	}
	/**
	 * Lis le contenu du dictionnaire personnel
	 * @return la liste des mots du dictionnaire personnel
	 */
	public Vector<String> read_dico() {
		Vector<String> res = new Vector<String>();
		File f = new File(Config.FICHIER_DICO_PERSO);
		
		Scanner sc;
		try {
			sc = new Scanner(f);
			String l;
			while(sc.hasNextLine()) {
				l=sc.nextLine();
				res.add(l);
				this.lm_dico.addElement(l);
			}
			sc.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return res;
	}
	/**
	 * Ajoute un mot au dictionnaire
	 * @param mot le ot à ajouter
	 */
	public void ajouterMot(String mot) {
		dico.ajouterMot(mot);
	}
	/**
	 * Supprime un mot du dictionnaire
	 * @param mot le mot à supprimer
	 */
	public void supprimerMot(String mot) {
		dico.supprimerMot(mot);
	}
}
class Event_JPDico implements ActionListener {
	JDico jp;
	/**
	 * Constructeur
	 * @param jp le panel du dictionnaire
	 */
	public Event_JPDico(JDico jp) {
		this.jp=jp;
	}
	/**
	 * Appelé lors de l'appui sur l'une des touches du panel
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object obj = arg0.getSource();
		if (obj instanceof JButton) {
			JButton jb = (JButton)obj;
			if (jb==jp.jb_ajout) {
				if (!jp.lm_dico.contains(jp.jtf_ajout.getText())) {
					// Ajout du mot dans le fichier + dans le Dico courant
					jp.ajouterMot(jp.jtf_ajout.getText());
					// Ajout du mot dans la liste JPDico
					jp.lm_dico.addElement(jp.jtf_ajout.getText());
					// Effacer le JTextField d'ajout de mot
					jp.jtf_ajout.setText(null);
				}
			} else if (jb==jp.jb_modifier) {
				if (jp.jlst_list.getSelectedIndex()!=-1) {
					// Supprimer le mot du fichier + du Dico courant
					jp.supprimerMot(jp.jlst_list.getSelectedValue().toString());
					// Déplacer le mot de la liste vers le JTextField d'ajout
					jp.jtf_ajout.setText(jp.jlst_list.getSelectedValue().toString());
					jp.lm_dico.remove(jp.jlst_list.getSelectedIndex());
				}
			} else if (jb==jp.jb_supprimer) {
				if (jp.jlst_list.getSelectedIndex()!=-1) {
					// Supprimer le mot du fichier + du Dico courant
					jp.supprimerMot(jp.jlst_list.getSelectedValue().toString());
					// Supprimer le mot de la liste
					jp.lm_dico.remove(jp.jlst_list.getSelectedIndex());
				}
			} else if (jb==jp.jb_select) {
				ActionEvent e = new ActionEvent(jp,1001,"JPDico_select");
				jp.getActionListener().actionPerformed(e);
			}
		}
	}
}